package game.login;

import game.util.DatabaseUtil;

import java.io.IOException;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbcp2.BasicDataSource;

import com.sun.org.apache.xml.internal.security.utils.Base64;

public class createUserAction extends HttpServlet{
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DatabaseUtil dbUtil = DatabaseUtil.getInstance();
		Connection conn = null;
		PreparedStatement pstmt = null;
		int count = 0;
		String sql = "insert into player values (?, ?, '사용자', 0, 0)";
		MessageDigest md = null;
		String id = (String) request.getParameter("id");
		String password = (String) request.getParameter("password");
		System.out.println("id = " + id + ", password = " + password);
		try {
			conn = dbUtil.getConnection();
			pstmt = conn.prepareStatement(sql);
			md = MessageDigest.getInstance("SHA-256");
			byte mdResult[] = md.digest(password.getBytes());
			String encPassword = Base64.encode(mdResult);
			pstmt.setString(1, id);
			pstmt.setString(2, encPassword);
			pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.close(conn, pstmt);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}